package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.PointTransInfo;
import com.eboss2.sbs.myapplication.MyApplication;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PointTransDetailActivity extends BaseActivity {

    private static final String TAG = "PointTransDetailActivity";
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_trans_type)
    TextView tvTransType;
    @BindView(R.id.tv_pay_way)
    TextView tvPayWay;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.tv_member_id)
    TextView tvMemberId;
    @BindView(R.id.tv_sale_count)
    TextView tvSaleCount;
    @BindView(R.id.tv_pay_time)
    TextView tvPayTime;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_order_amt)
    TextView tvOrderAmt;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.tv_point_deductible)
    TextView tvPointDeductible;
    @BindView(R.id.tv_point_get)
    TextView tvPointGet;
    @BindView(R.id.tv_give_coupon)
    TextView tvGiveCoupon;
    @BindView(R.id.tv_give_amt)
    TextView tvGiveAmt;
    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_give_wallet)
    TextView tvGiveWallet;


    private long tradeId;
    private long memberId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_point_trans_details_detail);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();

        Bundle bundle = getIntent().getBundleExtra("data");

        if (bundle != null) {
            tradeId = bundle.getLong("tradeId", 0);
            memberId = bundle.getLong("memberId", 0);
            if (tradeId != 0) {
                getdata();
            }
        }else {
            tradeId = getIntent().getLongExtra("tradeId", 0);
            memberId = getIntent().getLongExtra("memberId", 0);
            if (tradeId != 0) {
                getdata();
            }
        }

    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "交易详情 ");


    }


    @OnClick(R.id.titleback_btn_back)
    public void onViewClicked() {
        finish();
    }


    private void getdata() {

        showProgress("加载中...");
        RequestParams params = new RequestParams();
        params.put("tradeId", tradeId);
        params.put("memberId", memberId);
        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid());
        HttpUtil.get(Config.SBS_BONUS_TRANS_DETAILS, params, new HttpUtil.RequestListener() {
            @Override
            public void success(String response) {
                disShowProgress();
                try {
                    JSONObject result = new JSONObject(response);

                    if (!result.optBoolean("status")) {
                        ToastUtils.CustomShow(mContext, result.optString("message"));
                    } else {
                        PointTransInfo txRecordDetail = new Gson().fromJson(result.optString("data"), new TypeToken<PointTransInfo>() {
                        }.getType());
                        if (txRecordDetail != null) {
                            tvAmount.setText(StringUtils.formatIntMoney(txRecordDetail.getRealAmount()));
                            tvStatus.setText(txRecordDetail.getTransactionStatus());
                            tvTransType.setText(txRecordDetail.getTransactionType());
                            tvPayWay.setText(txRecordDetail.getPayType());
                            tvMemberName.setText(txRecordDetail.getPayerName());
                            tvMemberId.setText(txRecordDetail.getCardNo());
                            tvSaleCount.setText(txRecordDetail.getCustomerCount() + "");
                            tvPayTime.setText(txRecordDetail.getGmtCreate());
                            tvOrderNo.setText(txRecordDetail.getOrderId());
                            tvOrderAmt.setText(StringUtils.formatIntMoney(txRecordDetail.getOrderAmount())+"元");
                            tvCoupon.setText(StringUtils.formatIntMoney(txRecordDetail.getDiscountAmount()) + "元" + (StringUtils.isBlank(txRecordDetail.getCouponName()) ? "" : ("(" + txRecordDetail.getCouponName() + ")")));
                            tvPointDeductible.setText(StringUtils.formatIntMoney(txRecordDetail.getBonusValue()) + "元" + (txRecordDetail.getBonusAmount() == 0 ? "" : ("(" + txRecordDetail.getBonusAmount() + "积分)")));
                            tvPointGet.setText(txRecordDetail.getSendBonus() + "");
                            tvGiveCoupon.setText(StringUtils.formatIntMoney(txRecordDetail.getSendCouponAmount()) + "元" + (StringUtils.isBlank(txRecordDetail.getSendCouponName()) ? "" : ("(" + txRecordDetail.getSendCouponName() + ")")));
                            tvGiveAmt.setText(StringUtils.formatIntMoney(txRecordDetail.getCash()));
                            tvGiveWallet.setText(txRecordDetail.getRedpackageName());
                        }
                    }
                } catch (Exception e) {
                    Log.e(TAG, "doLogin() Exception: " + e.getMessage());
                }


            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }


}
